// pages/search/search.ts
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchArr: [],
    searchTitle:"",
    pickerIndex: 0,
    array: ['菜名', '评分', '价格'],
    inputValue: "",
    inputRemind: "请输入菜名...",
    resultNum:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '悦食生活-搜索结果',
    })
    //后端接口调整 3为饭堂 4为价格
    this.setData({
      pickerIndex:options.category-1,
      inputValue:options.data
    })
    if(options.category==3){options.category=4}
    console.log(options);
    this.getSearchResult(options)

  },

  getSearchResult: function (e) {
    wx.request({
      url: app.globalData.apiUrl + '/food/searchFood',
      data: {
        category: parseInt(e.category),
        data: e.data
        // category:2,
        // data:"2"
      },
      method: "POST",
      success: (result) => {
        console.log(result);
        var newSearchArr
        var newSite
        if(result.data.code==20001){
          this.setData({
            searchArr:[],
            resultNum:"无搜索结果"
          })
        }
        else if (!(result.data.data.data instanceof Array)) {
          this.setData({
            resultNum:"共1条搜索结果"
          })
          var newSearchArr = result.data.data
          console.log(newSearchArr);
          for (let index1 = 0; index1 < app.globalData.canteenList.length; index1++) {
            if (app.globalData.canteenList[index1].id == newSearchArr.data.canteenId) {
              newSite = app.globalData.canteenList[index1].canteenName
              break;
            }
          }
            var newSearchOne = [({
              canteenId: newSearchArr.data.canteenId,
              comment: newSearchArr.data.comment,
              foodClassId: newSearchArr.data.foodClassId,
              id: newSearchArr.data.id,
              imageUrl: newSearchArr.data.imageUrl,
              price: newSearchArr.data.price,
              tags: newSearchArr.data.tags,
              time: newSearchArr.data.time,
              title: newSearchArr.data.title,
              site: newSite,
            })]
            this.setData({
              searchArr: newSearchOne
            })         
        } 

        else {
          var newSearchArr = result.data.data.data
          this.setData({
            resultNum:"共"+newSearchArr.length+"条搜索结果"
          })
          console.log(result.data.data.data);
          for (let index = 0; index < newSearchArr.length; index++) {
            for (let index1 = 0; index1 < app.globalData.canteenList.length; index1++) {
              if (app.globalData.canteenList[index1].id == newSearchArr[index].canteenId) {
                newSite = app.globalData.canteenList[index1].canteenName
                break;
              }
            }
            var newSearchOne = ({
              canteenId: newSearchArr[index].canteenId,
              comment: newSearchArr[index].comment,
              foodClassId: newSearchArr[index].foodClassId,
              id: newSearchArr[index].id,
              imageUrl: newSearchArr[index].imageUrl,
              price: newSearchArr[index].price,
              tags: newSearchArr[index].tags,
              time: newSearchArr[index].time,
              title: newSearchArr[index].title,
              site: newSite,
            })
            newSearchArr[index] = newSearchOne
          }
          this.setData({
            searchArr: newSearchArr
          })
        }
      },
      fail: (err) => {},
      complete: (res) => {},
    })
  },

  pickerChange: function (e) {
    var tempText
    if (e.detail.value == 0) tempText = "请输入菜名..."
    else if (e.detail.value == 1) tempText = "评分不低于..."
    else if (e.detail.value == 2) tempText = "价格不高于..."
    this.setData({
      pickerIndex: e.detail.value,
      inputRemind: tempText
    })
  },

  inputChange: function (e) {
    // console.log(e.detail.value);
    this.setData({
      inputValue: e.detail.value
    })
  },
  goSearch() {
    if(this.data.inputValue!=""){
    let newCategory=parseInt(this.data.pickerIndex) + 1
    if(newCategory==3){newCategory=4}
    wx.request({
      url: app.globalData.apiUrl + '/food/searchFood',
      data: {
        category: newCategory,
        data: this.data.inputValue
      },
      method: "POST",
      success: (result) => {
        console.log(result);
        var newSearchArr
        var newSite
        if(result.data.code==20001){
          this.setData({
            searchArr:[],
            resultNum:"无搜索结果"
          })
        }
        else if (!(result.data.data.data instanceof Array)) {
          this.setData({
            resultNum:"共1条搜索结果"
          })
          var newSearchArr = result.data.data
          console.log(newSearchArr);
          for (let index1 = 0; index1 < app.globalData.canteenList.length; index1++) {
            if (app.globalData.canteenList[index1].id == newSearchArr.data.canteenId) {
              newSite = app.globalData.canteenList[index1].canteenName
              break;
            }
          }
            var newSearchOne = [({
              canteenId: newSearchArr.data.canteenId,
              comment: newSearchArr.data.comment,
              foodClassId: newSearchArr.data.foodClassId,
              id: newSearchArr.data.id,
              imageUrl: newSearchArr.data.imageUrl,
              price: newSearchArr.data.price,
              tags: newSearchArr.data.tags,
              time: newSearchArr.data.time,
              title: newSearchArr.data.title,
              site: newSite,
            })]
            this.setData({
              searchArr: newSearchOne
            })         
        } 

        else {
          var newSearchArr = result.data.data.data
          this.setData({
            resultNum:"共"+newSearchArr.length+"条搜索结果"
          })
          console.log(result.data.data.data);
          for (let index = 0; index < newSearchArr.length; index++) {
            for (let index1 = 0; index1 < app.globalData.canteenList.length; index1++) {
              if (app.globalData.canteenList[index1].id == newSearchArr[index].canteenId) {
                newSite = app.globalData.canteenList[index1].canteenName
                break;
              }
            }
            var newSearchOne = ({
              canteenId: newSearchArr[index].canteenId,
              comment: newSearchArr[index].comment,
              foodClassId: newSearchArr[index].foodClassId,
              id: newSearchArr[index].id,
              imageUrl: newSearchArr[index].imageUrl,
              price: newSearchArr[index].price,
              tags: newSearchArr[index].tags,
              time: newSearchArr[index].time,
              title: newSearchArr[index].title,
              site: newSite,
            })
            newSearchArr[index] = newSearchOne
          }
          this.setData({
            searchArr: newSearchArr
          })
        }
      },
      fail: (err) => {},
      complete: (res) => {},
    })

  }
  else{
    wx.showToast({
      title: '输入内容为空',
      icon:"error"
    })
  }
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})